package cn.com.dev;

import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @projectName: ngsapitask-bp
 * @package: cn.com.dev
 * @className: HmacTest
 * @author: LiJT
 * @description: TODO
 * @date: 2024-09-02 13:58
 * @version: 1.0
 */
public class WXTest {
    private static final Log log = LogFactory.getLog(Test_API.class);

    // 测试
    public static void main(String[] args) {

        String js_code = "js_codejs_code";
        String getResult = "{\n" + "\"openid\":\"openid\",\n" + "\"session_key\":\"session_key\",\n" + "\"unionid\":\"unionid\",\n" + "\"errcode\":0,\n" + "\"errmsg\":\"errmsg\"\n" + "}";

        JSONObject loginObject = new JSONObject(getResult);
                /*
                String openid = loginObject.get("openid");
                String session_key = loginObject.get("session_key");
                String unionid = loginObject.get("unionid");
                String errcode = loginObject.get("errcode");
                String errmsg = loginObject.get("errmsg");
                */

        String openid = GetJV(loginObject, "openid");
        String session_key = GetJV(loginObject, "session_key");
        String unionid = GetJV(loginObject, "unionid");
        String errcode = GetJV(loginObject, "errcode");
        String errmsg = GetJV(loginObject, "errmsg");

        // 记录验证结果
        JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String log_SQL = "Insert Into Kh_Minip_Login (Rec_Id, Js_Code, Open_Id, Unionid, Session_Key, Errcode, Errmsg, Createdtime) Values (?, ?, ?, ?, ?, ?, ?, ?)";
        jt.update(log_SQL, KHUtil.getRandomString(36), js_code, openid, unionid, session_key, errcode, errmsg, df.format(new Date()));


        // 检查是否是有效OpenID
        String selectSQL = "Select Rec_Id, Js_Code, Open_Id, Unionid, Session_Key, Errcode, Errmsg From Kh_Minip_Login Where Open_Id = ? Order By Createdtime Desc Limit 1";
        List<Map<String, Object>> selectExits = jt.queryForList(selectSQL, openid);
        log.debug("☑获取-selectExits :" + selectExits);
        if (selectExits.size() > 0) {
            log.debug("☑获取-存在 :" + selectExits.size());

        }

    }

    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        return Rev_Value;
    }
}
